<template>
  <el-dialog
    :title="!dataForm.attrGroupId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    @closed="dialogClosed"
  >
    <el-form
      :model="dataForm"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="100px"
    >
      <el-form-item label="属性名" prop="attrName">
        <el-input v-model="dataForm.attrName" placeholder="属性名"></el-input>
      </el-form-item>
      <el-form-item label="属性类型" prop="attrType">
        <el-select v-model="dataForm.attrType" filterable placeholder="请选择">
          <el-option
            v-for="item in attrTypeOptions"
            :key="item.attrGroupId"
            :label="item.attrGroupName"
            :value="item.attrGroupId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="值类型" prop="valueType">
        <el-switch
          v-model="dataForm.valueType"
          active-text="允许多个值"
          inactive-text="只能单个值"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="可选值" prop="valueSelect">
        <el-input
          v-model="dataForm.valueSelect"
          placeholder="可选值"
        ></el-input>
      </el-form-item>
      <el-form-item label="属性图标" prop="icon">
        <el-input v-model="dataForm.icon" placeholder="属性图标"></el-input>
      </el-form-item>
      <el-form-item label="所属分类id" prop="catelogId">
        <el-cascader
          v-model="dataForm.catelogPath"
          :options="categorys"
          :props="props"
          filterable
        ></el-cascader>
      </el-form-item>
      <el-form-item label="所属分组" prop="groupId">
        <el-select v-model="dataForm.groupId" filterable placeholder="请选择">
          <el-option
            v-for="item in groupIdOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="可检索" prop="icon">
        <el-switch
          v-model="dataForm.searchType"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="快速展示" prop="icon">
        <el-switch
          v-model="dataForm.showDesc"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
        >
        </el-switch>
      </el-form-item>
      <el-form-item label="启用状态" prop="icon">
        <el-switch
          v-model="dataForm.enable"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :inactive-value="0"
          :active-value="1"
        >
        </el-switch>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      props: {
        value: "catId",
        label: "name",
        children: "children",
      },
      categorys: [],
      visible: false,
      dataForm: {
        attrId: 0,
        attrName: "",
        attrType: "",
        valueType: "",
        valueSelect: "",
        icon: "",
        catelogPath: [],
        groupId: 0,
        searchType: false,
        showDesc: false,
        enable: false,
      },
      attrTypeOptions: [],
      groupIdOptions: [],
    };
  },
  methods: {
    attrTypeSelect(attrGroupId) {
      this.$http({
        url: this.$http.adornUrl(`/product/attrgroup/listAll/${attrGroupId}`),
        method: "get",
      }).then(({ data }) => {
        // console.log(data);
        this.attrTypeOptions = data.list;
      });
    },
    dialogClosed() {
      this.dataForm.catelogPath = [];
    },
    getCategorys() {
      this.$http({
        url: this.$http.adornUrl("/product/category/list"),
        method: "get",
      }).then(({ data }) => {
        this.categorys = data.data;
      });
    },
    init(id) {
      this.dataForm.attrGroupId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.attrGroupId) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/attrgroup/info/${this.dataForm.attrGroupId}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.attrGroupName = data.attrGroup.attrGroupName;
              this.dataForm.sort = data.attrGroup.sort;
              this.dataForm.descript = data.attrGroup.descript;
              this.dataForm.icon = data.attrGroup.icon;
              this.dataForm.catelogId = data.attrGroup.catelogId;
              this.dataForm.catelogPath = data.attrGroup.catelogPath;
            }
          });
        }
      });
      this.attrTypeSelect(this.dataForm.attrGroupId)
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/product/attr/${!this.dataForm.attrId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              attrId: this.attrId,
              attrName: this.dataForm.attrName,
              attrType: this.dataForm.attrType,
              valueType: this.dataForm.valueType,
              valueSelect: this.dataForm.valueSelect,
              icon: this.dataForm.icon,
              catelogId:
                this.dataForm.catelogPath[this.dataForm.catelogPath.length - 1],
              groupId: this.dataForm.groupId,
              searchType: this.dataForm.searchType,
              showDesc: this.dataForm.showDesc,
              enable: this.dataForm.enable,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
  created() {
    this.getCategorys();
  },
};
</script>
